<template>
  <div class="login-container">
    <div class="login-form-wrapper">
      <h1 class="login-title">心理测评</h1>
      <p class="login-subtitle">请使邮箱和密码登录</p>

      <form @submit.prevent="handleLogin" class="login-form">
        <div class="form-group">
          <div class="input-wrapper">
            <svg class="icon" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
              <path
                d="M21 12C21 16.9706 16.9706 21 12 21C7.02944 21 3 16.9706 3 12C3 7.02944 7.02944 3 12 3C16.9706 3 21 7.02944 21 12Z"
                stroke="currentColor"
                stroke-width="2"
                stroke-linecap="round"
                stroke-linejoin="round"
              />
              <path
                d="M12 8V12L14 14"
                stroke="currentColor"
                stroke-width="2"
                stroke-linecap="round"
                stroke-linejoin="round"
              />
            </svg>
            <input
              v-model="formData.email"
              type="email"
              placeholder="邮箱"
              class="form-input"
              required
            />
          </div>
        </div>

        <div class="form-group">
          <div class="input-wrapper">
            <svg class="icon" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
              <path
                d="M16 21V19C16 17.9391 15.5786 16.9217 14.8284 16.1716C14.0783 15.4214 13.0609 15 12 15H8C6.93913 15 5.92172 15.4214 5.17157 16.1716C4.42143 16.9217 4 17.9391 4 19V21"
                stroke="currentColor"
                stroke-width="2"
                stroke-linecap="round"
                stroke-linejoin="round"
              />
              <path
                d="M12 12C13.6569 12 15 10.6569 15 9C15 7.34315 13.6569 6 12 6C10.3431 6 9 7.34315 9 9C9 10.6569 10.3431 12 12 12Z"
                stroke="currentColor"
                stroke-width="2"
                stroke-linecap="round"
                stroke-linejoin="round"
              />
              <path
                d="M8.5 8.5L7.5 7.5"
                stroke="currentColor"
                stroke-width="2"
                stroke-linecap="round"
                stroke-linejoin="round"
              />
              <path
                d="M16.5 8.5L15.5 7.5"
                stroke="currentColor"
                stroke-width="2"
                stroke-linecap="round"
                stroke-linejoin="round"
              />
            </svg>
            <input
              v-model="formData.password"
              :type="showPassword ? 'text' : 'password'"
              placeholder="密码"
              class="form-input"
              required
            />
            <button
              type="button"
              class="toggle-password"
              @click="togglePassword"
              aria-label="切换密码可见性"
            >
              <svg
                v-if="showPassword"
                class="icon"
                viewBox="0 0 24 24"
                fill="none"
                xmlns="http://www.w3.org/2000/svg"
              >
                <path
                  d="M13.73 21a2 2 0 0 1-3.46 0"
                  stroke="currentColor"
                  stroke-width="2"
                  stroke-linecap="round"
                  stroke-linejoin="round"
                />
                <path
                  d="M12 12c-1.09 0-2.18.16-3.22.46m6.44 0C14.18 12.16 13.09 12 12 12c-1.09 0-2.18.16-3.22.46M9.13 17.56a8 8 0 1 1 7.75 0M3 12h18"
                  stroke="currentColor"
                  stroke-width="2"
                  stroke-linecap="round"
                  stroke-linejoin="round"
                />
              </svg>
              <svg
                v-else
                class="icon"
                viewBox="0 0 24 24"
                fill="none"
                xmlns="http://www.w3.org/2000/svg"
              >
                <path
                  d="M2 12s3-7 10-7 10 7 10 7-3 7-10 7-10-7-10-7Z"
                  stroke="currentColor"
                  stroke-width="2"
                  stroke-linecap="round"
                  stroke-linejoin="round"
                />
                <path
                  d="M12 16v-4"
                  stroke="currentColor"
                  stroke-width="2"
                  stroke-linecap="round"
                  stroke-linejoin="round"
                />
                <path
                  d="M12 8h.01"
                  stroke="currentColor"
                  stroke-width="2"
                  stroke-linecap="round"
                  stroke-linejoin="round"
                />
              </svg>
            </button>
          </div>
        </div>

        <button type="submit" class="login-button" :disabled="loading">
          {{ loading ? '登录中...' : '登录' }}
        </button>

        <div class="form-footer">
          <router-link to="/register" class="sign-up-button">注册 →</router-link>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
import { ref, reactive, onBeforeUnmount } from 'vue'
import { useUserStore } from '@/stores/user'
import { useRouter } from 'vue-router'
import { storeToRefs } from 'pinia'

export default {
  name: 'LoginView',
  setup() {
    const userStore = useUserStore()
    const router = useRouter()
    const { loading, error } = storeToRefs(userStore)

    // 使用reactive创建响应式的formData对象
    const formData = reactive({
      email: '',
      password: '',
    })

    // 使用ref创建响应式的showPassword变量
    const showPassword = ref(false)

    // 在setup中定义togglePassword函数
    const togglePassword = () => {
      showPassword.value = !showPassword.value
    }

    // 在setup中定义handleLogin函数
    const handleLogin = async () => {
      userStore.clearError()

      try {
        await userStore.login(formData)

        // 显示成功消息
        alert('登录成功！')

        // 跳转到测评列表页面
        router.push('/assessment')
      } catch (err) {
        // 错误已在store中处理
        console.error('Login failed:', err)
        alert(error.value || '登录失败，请检查邮箱和密码')
      }
    }

    // 使用onBeforeUnmount注册生命周期钩子
    onBeforeUnmount(() => {
      // 组件卸载时清除错误
      userStore.clearError()
    })

    return {
      formData,
      showPassword,
      loading,
      error,
      userStore,
      router,
      togglePassword,
      handleLogin,
    }
  },
}
</script>

<style scoped>
/* 登录容器 - 全屏flex布局 */
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-image: url('@/assets/img/psychology.jpg');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  padding: 20px;
  width: 100%;
  box-sizing: border-box;
  margin: 0;
  position: relative;
}

/* 登录表单包装器 - 响应式卡片 */
.login-form-wrapper {
  background: linear-gradient(135deg, #d4f0f0 0%, #fccbfa 100%);
  border-radius: 16px;
  padding: 40px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  max-width: 400px;
  width: 100%;
  position: relative;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.login-form-wrapper::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: linear-gradient(
    45deg,
    rgba(255, 255, 255, 0.1) 0%,
    rgba(255, 255, 255, 0) 50%,
    rgba(255, 255, 255, 0.1) 100%
  );
  transform: rotate(45deg);
  animation: shimmer 2s infinite;
}

@keyframes shimmer {
  0% {
    transform: translateX(-100%) rotate(45deg);
  }
  100% {
    transform: translateX(100%) rotate(45deg);
  }
}

.login-title {
  font-size: 28px;
  font-weight: bold;
  text-align: center;
  margin-bottom: 8px;
  color: #333;
}

.login-subtitle {
  font-size: 14px;
  text-align: center;
  color: #666;
  margin-bottom: 30px;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.form-group {
  width: 100%;
}

.input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 8px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  padding: 0 15px;
  transition: all 0.3s ease;
}

.input-wrapper:focus-within {
  border-color: #61dafb;
  box-shadow: 0 0 0 3px rgba(97, 218, 251, 0.1);
}

.icon {
  width: 18px;
  height: 18px;
  color: #888;
  margin-right: 10px;
  flex-shrink: 0;
}

.form-input {
  flex-grow: 1;
  padding: 14px 10px;
  border: none;
  outline: none;
  background: transparent;
  font-size: 16px;
  color: #333;
}

.form-input::placeholder {
  color: #aaa;
}

.toggle-password {
  background: none;
  border: none;
  cursor: pointer;
  padding: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity 0.3s ease;
}

.toggle-password:hover {
  opacity: 0.8;
}

.login-button {
  background: linear-gradient(135deg, #61dafb 0%, #4fc0e8 100%);
  color: white;
  border: none;
  padding: 14px;
  border-radius: 8px;
  font-size: 16px;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-top: 10px;
}

.login-button:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(97, 218, 251, 0.3);
}

.login-button:active:not(:disabled) {
  transform: translateY(0);
}

.login-button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
}

.form-footer {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  margin-top: 5px;
}

.sign-up-button {
  background: rgba(255, 255, 255, 0.9);
  color: #61dafb;
  border: none;
  padding: 8px 16px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: bold;
  text-decoration: none;
  cursor: pointer;
  transition: all 0.3s ease;
  width: 100%;
  text-align: center;
}

.sign-up-button:hover {
  background: white;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

/* H5端响应式适配 */
@media (max-width: 768px) {
  .login-container {
    padding: 10px;
  }

  .login-form-wrapper {
    padding: 30px 20px;
    max-width: 100%;
  }

  .login-title {
    font-size: 24px;
  }

  .login-subtitle {
    font-size: 13px;
  }

  .login-form {
    gap: 15px;
  }

  .form-input {
    padding: 12px 10px;
    font-size: 15px;
  }

  .login-button {
    padding: 12px;
    font-size: 15px;
  }

  .form-footer {
    flex-direction: column;
    gap: 10px;
    align-items: center;
  }

  .sign-up-button {
    font-size: 13px;
  }
}
</style>
